package com.zzptc.syl.traffic;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RightFragment extends Fragment {
    private ListView listView;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private List<Getcarno.DataBean.LogBean> list=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_right, container, false);
        listView=view.findViewById(R.id.lv_details);
        getViolationData(AppData.getInstance().getCarno());
        return view;
    }
    private void getViolationData(String car){
        Map<String,Object> data1=new HashMap<>();
        data1.put("CarNo","鲁"+car);
        Carservice carservice=retrofit.create(Carservice.class);
        Call<Getcarno> call=carservice.getweizhang(data1);
        call.enqueue(new Callback<Getcarno>() {
            @Override
            public void onResponse(Call<Getcarno> call, Response<Getcarno> response) {
                list.addAll(response.body().getData().get(0).getLog());
                MyAdapter myAdapter=new MyAdapter();
                listView.setAdapter(myAdapter);
            }

            @Override
            public void onFailure(Call<Getcarno> call, Throwable t) {

            }
        });
    }
    public class MyAdapter extends BaseAdapter{
        private TextView time;
        private TextView not;
        private TextView intersection;
        private TextView book;
        private TextView fraction;
        private TextView fine;
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view=LayoutInflater.from(getContext()).inflate(R.layout.violation,viewGroup,false);
            time=view.findViewById(R.id.tv_time);
            not=view.findViewById(R.id.tv_not);
            intersection=view.findViewById(R.id.tv_intersection);
            book=view.findViewById(R.id.tv_book);
            fraction=view.findViewById(R.id.tv_fraction);
            fine=view.findViewById(R.id.tv_fine);
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
            format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            String date=format.format(new Date());
            time.setText(date);
            if (list.get(i).isProcessed()==false){
                not.setText("未处理");
            }
            intersection.setText(list.get(i).getAddress());
            book.setText(list.get(i).getReason());
            fraction.setText("扣："+list.get(i).getMark()+"分");
            fine.setText("罚款："+list.get(i).getFee()+"元");
            return view;
        }
    }
}